﻿using UnityEngine;
using UnityEngine.UI;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using SimpleJSON;

public class Main : MonoBehaviour {

	// Use this for initialization
    string csvPath = "";
    public Text pathText;
    public GameObject messageBox;
	void Start () {
        ShowMessageBox(false);
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void SelectFile()
    {
        OpenFileName ofn = new OpenFileName();
        ofn.structSize = Marshal.SizeOf(ofn);
        ofn.filter = "CSV\0*.csv\0\0";
        ofn.file = new string(new char[256]);
        ofn.maxFile = ofn.file.Length;
        ofn.fileTitle = new string(new char[64]);
        ofn.maxFileTitle = ofn.fileTitle.Length;
        ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
        ofn.title = "Open File";
        ofn.defExt = "excel";//显示文件的类型
        //注意 一下项目不一定要全选 但是0x00000008项不要缺少
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR

        if (WindowDll.GetOpenFileName(ofn)) {
            if (ofn.file == null || ofn.file.Length < 1)
                return;
            Debug.Log("Selected file with full path: {0}" + ofn.file);
            csvPath = ofn.file;
            pathText.text = csvPath;
        }
    }

    public void ToJson()
    {
        string jsonData = readExcelData(csvPath);
        outJsonContentToFile(jsonData, Path.ChangeExtension(csvPath, "json"));
    }

    string readExcelData(string fileName)
    {
        if (!File.Exists(fileName)) {
            return null;
        }
        string fileContent = File.ReadAllText(fileName, UnicodeEncoding.Default);
        string[] fileLineContent = fileContent.Split(new string[] { "\r\n" }, System.StringSplitOptions.None);
        if (fileLineContent != null) {
            //注释的名字
            string[] noteContents = fileLineContent[0].Split(new string[] { "," }, System.StringSplitOptions.None);
            //变量的名字
            string[] VariableNameContents = fileLineContent[1].Split(new string[] { "," }, System.StringSplitOptions.None);

            JSONClass jsonData = new JSONClass();
            for (int i = 2; i < fileLineContent.Length - 1; i++) {
                string[] lineContents = fileLineContent[i].Split(new string[] { "," }, System.StringSplitOptions.None);
                JSONClass classLine = new JSONClass();
                for (int j = 1; j < lineContents.Length; j++) {
                    classLine[VariableNameContents[j]] = lineContents[j];
                }
                jsonData[lineContents[0]] = classLine;
            }

            string resultJson = jsonData.ToString("");
            return resultJson;
        }
        return null;
    }

    void outJsonContentToFile(string jsonData, string jsonFilePath)
    {
        string directName = Path.GetDirectoryName(jsonFilePath);
        if (!Directory.Exists(directName)) {
            Directory.CreateDirectory(directName);
        }
        File.WriteAllText(jsonFilePath, jsonData, Encoding.UTF8);
        Debug.Log("成功输出Json文件  :" + jsonFilePath);
        ShowMessageBox(true);
    }

    public void ShowMessageBox(bool isShow)
    {
        messageBox.SetActive(isShow);
    }
}
